import React, {useState} from 'react'
import { register } from '../api/user'
import { message, Button, Form, Input } from 'antd';

const Register = () => {
    const [loading, setLoading] = useState(false)
    const onFinish = (values) => {
        setLoading(true);
        register(values)
            .then(res => {
                if (res.code == 200) {
                    message.success('注册成功')
                }
            })
            .finally(()=>{
                setLoading(false);
            })
    }
    return (
        <div>
            <Form
                name="basic"
                layout='vertical'
                style={{
                    maxWidth: 400,
                    margin: '50px auto'
                }}
                onFinish={onFinish}
                autoComplete="off"
            >
                <Form.Item
                    label="用户名"
                    name="userName"
                    rules={[
                        {
                            required: true,
                            message: 'Please input your username!',
                        },
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="密码"
                    name="password"
                    hasFeedback
                    rules={[
                        {
                            required: true,
                            message: 'Please input your password!',
                        },
                    ]}
                >
                    <Input.Password />
                </Form.Item>
                <Form.Item
                    label="确认密码"
                    name="confirm"
                    dependencies={['password']}
                    hasFeedback
                    rules={[
                        {
                            required: true,
                            message: 'Please input your password!',
                        },
                        ({ getFieldValue }) => ({
                            validator(_, value) {
                                if (!value || getFieldValue('password') === value) {
                                    return Promise.resolve();
                                }
                                return Promise.reject(new Error('The two passwords that you entered do not match!'));
                            },
                        }),
                    ]}
                >
                    <Input.Password />
                </Form.Item>
                <Form.Item>
                    <Button block shape='round' loading={loading} type="primary" htmlType="submit">
                        注册
                    </Button>
                </Form.Item>
            </Form>
        </div>
    )
}

export default Register